<script name="CallbackBind" lang="ts" setup>
//绑定手机
import userStore from '@/store/index'
import  Message from "@/components/message/index"
import { useRouter } from 'vue-router'
import {QQUserInfo ,QQUserInfoRes } from "@/types/user"
import { ref } from 'vue';
import { useCountDown } from "@/utils/hooks"
// 表单校验---------------------------------------------
import { useField , useForm } from 'vee-validate' //插件
import {
  mobileRule,
  codeRule,
} from '@/utils/validate'
const { validate } = useForm({
  validationSchema: {
    mobile: mobileRule,
    code: codeRule
  }
})
const { value: mobile, errorMessage: mobileError } = useField<string>('mobile')
const { value: code, errorMessage: codeError } = useField<string>('code')
const qqInfo = ref<QQUserInfo>({} as QQUserInfo) //存qq数据
const { user }=userStore()
const router =useRouter()
//--------------------------------------------------------
// 有账号，绑定过---------------------------------------------
// 获取用户信息
const isLogin = QC.Login.check()
// console.log(isLogin)
if (isLogin) {
  // 2. 获取openId
  QC.Login.getMe(async(openId) => {
    // console.log(openId)
     // 3. 发送请求进行QQ登录
    //  console.log('下一步发请求');
    const res= await user.qqLogin(openId)
    // console.log(res);
    Message.success('登录成功')
    router.push('/')
  })
}
//--------------------------------------------------------
// 有账号，没绑定---------------------------------------------
let openId =''
//1判断qq是否登录
if(QC.Login.check()){
  // 获取qq用户信息
  QC.api('get_user_info').success((res:QQUserInfoRes)=>{
    // console.log('qq用户信息',res);
     qqInfo.value = res.data
  })
   // 3. 获取openId
  QC.Login.getMe((id) => {
    openId = id
  })
}
//--------------------------------------------------------
const { time, start } = useCountDown(5)
const send = async () => {
  if (time.value > 0) return
  // // 发送请求绑定qq
  await user.sendQQBindMsg(mobile.value)
  Message.success('发送验证码成功')
  // // 开启倒计时
  start()
}
const bind = async () => {
  const res = await validate()
  // console.log(res);
  if (!res.valid) return
    // 如果校验，发送请求进行绑定
    // console.log('绑定');
  await user.qqBindLogin(openId, mobile.value, code.value)
  Message.success('QQ绑定成功')
  router.push('/')
}
//--------------------------------------------------------
// QQ登录-未绑定-无账号--------------------------------------

//--------------------------------------------------------


</script>
<template>
  <div class="xtx-form">
    <div class="user-info">
      <img
        :src="qqInfo.figureurl_qq_2"
        alt=""
      />
      <p>Hi，{{qqInfo.nickname}} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input class="input" type="text"  v-model="mobile" placeholder="绑定的手机号" />
      </div>
      <div class="error">{{mobileError}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input  v-model="code" class="input" type="text" placeholder="短信验证码" />
        <span class="code" @click="send">
          {{ time === 0 ? '发送验证码' : `${time}s后发送` }}</span>
      </div>
      <div class="error">{{codeError}}</div>
    </div>
    <a href="javascript:;" class="submit"  @click="bind">立即绑定</a>
  </div>
</template>

<style scoped lang="less">
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>

